#ifndef STACKINT_H
#define STACKINT_H

#include <stdio.h>
#include <stdlib.h>

#define NONVOID 0
#define OVERFLOW 1
#define NONDEFINE 2
#define INVALID 3
#define NOTFOUND 404

#define TRUE 1
#define FALSE 0

typedef int bool;
typedef int ElementType;
typedef struct SNode *PtrToStack;
struct SNode{
    ElementType *data;
    int Top;
    int Maxsize;
};
typedef PtrToStack Stack;

// Create an empty stack the largest length of which is MAXSIZE.
Stack CreateStack(int maxSize);

// Judge whether stack S is full or not.
bool StackFull(Stack S);

// void Push(Stack S, ElementType item);
// Push element item into the stack.
void Push(Stack S, ElementType item);

// Judge whether Stack S is empty or not.
bool StackEmpty(Stack S);

// ElementType Pop(Stack S);
// Delete and return the top element of stack.
ElementType Pop(Stack S);

// void ShowList(Stack S);

#endif // STACKINT_H

Stack CreateStack(int maxSize){
    Stack S = (Stack) malloc(sizeof(struct SNode*));
    S->data = (ElementType *) malloc(sizeof(ElementType*) * maxSize);
    S->Top = -1;
    S->Maxsize = maxSize;
    return S;
}

bool StackFull(Stack S){
    if(S->Top == S->Maxsize - 1)
        return TRUE;
    else
        return FALSE;
}

void Push(Stack S, ElementType item){
    if(S->Top == S->Maxsize - 1){
        printf("Operation Failed!\nError Code %02x!\n",OVERFLOW);
        return;
    }
    else{
        S->data[++(S->Top)] = item;
    }
}

bool StackEmpty(Stack S){
    if(S->Top == -1)
        return TRUE;
    else
        return FALSE;
}

ElementType Pop(Stack S){
    if(S->Top == -1){
        printf("Operation Failed!\nError Code %02x!\n",INVALID);
        return NULL;
    }
    else{
        return S->data[(S->Top)--];
    }
}

